##############################################
# OpenWrt Makefile for bootloader
#
##############################################


include $(TOPDIR)/rules.mk

# Build in kernel build dir
include $(INCLUDE_DIR)/kernel.mk

ifeq ($(PR_NAME),)
$(error PR_NAME should not be empty)
endif

# Name and release number of this package
PKG_NAME:=bootloader
PKG_RELEASE:=1

# this package type is bin not ipkg, no ipk needed
# normal boot & factory boot is all set to bin type
PKG_TARGETS:=bin

# This specifies the directory where we're going to build the program.
PKG_BUILD_DIR:=$(KERNEL_BUILD_DIR)/$(PKG_NAME)
# normalboot 、dbg_normal_boot 和 factoryboot分别在三个子目录下编译
FACTORYBOOT_BUILD_DIR:=$(PKG_BUILD_DIR)/factory_boot

PKG_SRC_DIR:=$(subst ",,$(CONFIG_BOOT_SRC_DIR))

#used in factory boot
COMMON_SRC_DIR:=shared_libs

include $(INCLUDE_DIR)/package.mk

define Package/bootloader
  SECTION:=boot
  CATEGORY:=Boot Loader
  TITLE:=bootloader for NVMP
  MENU:=1
endef

FACTORY_BOOT_IMAGE:=$(if $(IMAGE),$(IMAGE),slp-$(BOARD)-$(PROFILE)-factory_boot.bin)

# 芯片源码在合并uboot与factory uboot后分为src目录和configs目录
# src存放合并的源码，configs目录存放uboot与factory boot需要传递的宏，
# 【需要定义的宏以后均放在这两个文件中】
# 分为normalboot.mk(uboot需要的宏)与factoryboot.mk(factory boot需要的宏)
ifdef PKG_SRC_DIR

# NORMALBOOT_MAKEOPTS used to compile normalboot

# DBG_NORMALBOOT_MAKEOPTS used to compile normalboot

#FACTORYBOOT_MAKEOPTS used to compile factoryboot
include $(PKG_SRC_DIR)/configs/factoryboot.mk

endif


# if COMPRESSED_UBOOT is 1, then tuboot.bin is the final binary file.
# if COMPRESSED_UBOOT is 0, then u-boot.bin is the final binary file.
# the value of the parameter "COMPRESSED_UBOOT" is got from parsing the
# variant "NORMALBOOT_MAKEOPTS"、"DBG_NORMALBOOT_MAKEOPTS" or "FACTORYBOOT_MAKEOPTS" which is defined above.

COMPRESSED_FACTORY_BOOT=$(patsubst COMPRESSED_UBOOT=%,%,$(filter COMPRESSED_UBOOT=%, $(FACTORYBOOT_MAKEOPTS)))
ifeq ($(COMPRESSED_FACTORY_BOOT),y)
	COMPRESSED_FACTORY_BOOT=1
endif
ifeq ($(COMPRESSED_FACTORY_BOOT),1)
	FACTORY_BOOT_BINARY:=factory_tuboot.bin
else
	FACTORY_BOOT_BINARY:=factory_u-boot.bin
endif

# 由于根据芯片方案有差异define Build/Compile和Package/bootloader/install
# 被放到芯片方案的boot/configs/compile.mk中
# 【目前Build/Prepare和Build/Configure也被放进去】
ifdef PKG_SRC_DIR
	include $(PKG_SRC_DIR)/configs/compile.mk
endif

# Specify what needs to be done to prepare for building the package.
# In our case, we need to copy the source files to the build directory.
# This is NOT the default.  The default uses the PKG_SOURCE_URL and the
# PKG_SOURCE which is not defined here to download the source from the web.
define Build/Prepare
	$(Build/Prepare/factory_boot)
endef

define Build/Configure
	$(Build/Configure/factory_boot)
endef

define Build/Compile
	$(Build/Compile/factory_boot)
endef

define Package/bootloader/install
	$(Package/bootloader/install/factory_boot)
endef

$(eval $(call BuildPackage,bootloader))
